/**
 * Lenovo Group
 * Copyright (c) 1999-2016 All Rights Reserved.
 */
package com.lenovo.lmrp.api.model;

import com.lenovo.sdc.framework.util.JSONHelper;
import com.lenovo.sdc.framework.util.exception.ErrorCode;
import com.lenovo.sdc.framework.util.exception.TraceableException;

/**
 * 
 * @author wujin
 * @version $Id: RestResponse.java, v 0.1 2016年1月14日 上午10:49:47 wujin Exp $
 */
public class RestResponse <T> {
    private int ecode;
    private String reason;
    private T result;
    
    /**
     * 
     */
    public RestResponse() {
        // TODO Auto-generated constructor stub
    }
    
    /**
     * default ecode == 0, reason == OK
     * @param result
     */
    public RestResponse(T result) {
        this(0, "OK", result);
    }
    
    /**
     * @param ecode
     * @param reason
     * @param result
     */
    public RestResponse(int ecode, String reason, T result) {
        super();
        this.ecode = ecode;
        this.reason = reason;
        this.result = result;
    }


    /**
     * Getter method for property <tt>ecode</tt>.
     * 
     * @return property value of ecode
     */
    public int getEcode() {
        return ecode;
    }

    /**
     * Getter method for property <tt>reason</tt>.
     * 
     * @return property value of reason
     */
    public String getReason() {
        return reason;
    }

    /**
     * Getter method for property <tt>result</tt>.
     * 
     * @return property value of result
     */
    public T getResult() {
        return result;
    }

    /**
     * help to convert generic type
     * 
     * @param clz
     * @return
     */
    public T getResult(Class<T> clz) {
        try {
            return JSONHelper.convertToObject(result, clz);
        }
        catch (Exception exp) {
            throw new TraceableException(ErrorCode.COMM_PARAM_FORMAT, exp);
        }
    }
}
